package com.hinnotek.warehouse.ui.fragment

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.MenuBean
import com.hinnotek.warehouse.databinding.FragmentHomeBinding
import com.hinnotek.warehouse.databinding.ItemWarehouseMenuBinding
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseToolbarFragment
import com.lee.common.data.AppDatabase
import com.lee.common.utils.*
import timber.log.Timber

private const val KEY_TITLE = "key_title"

@Route(path = FRAGMENT_HOME)
class HomeFragment : BaseToolbarFragment<FragmentHomeBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MenuBean, ItemWarehouseMenuBinding>

    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }

    companion object {
        fun newInstance(title: String): HomeFragment {
            val args = Bundle()
            args.putString(KEY_TITLE, title)
            val fragment = HomeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initData() {

        setTitle("主页")
        hideLiftBack()
        val token = AppDatabase.getInstance(requireContext()).userDao().getUser()?.token ?: ""
        Timber.e("token:$token")
        adapter = SimpleBindingAdapter(R.layout.item_warehouse_menu, ArrayList(), BR.warehouseMenu)
        mBinding.mainRecycler.layoutManager = object : GridLayoutManager(requireContext(), 3) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        mBinding.mainRecycler.adapter = adapter

        val isWarehouse = SharedPreUtils.getBoolean(isAmoebaWareHouse, false)
        adapter.setNewInstance(MenuBean.getData2())
        adapter.setOnItemClickListener { _, _, position ->
            when (this.adapter.data[position].title) {


                "仓库管理" -> {
                    val showWarehouse = SharedPreUtils.getBoolean(isAmoebaWareHouse, false)
                    if (!showWarehouse) {
                        ToastUtils.showShort("没有相关权限!")
                        return@setOnItemClickListener
                    }
                    Skipping.start(ACTIVITY_WAREHOUSE_MAIN)
                }
                "料号信息" -> {
                    //Skipping.start(ACTIVITY_MAIN_OLD)
                }
                "技术管理" -> {
                    //Skipping.start(ACTIVITY_MAIN_OLD)
                }
                "销售管理" -> {
                    //Skipping.start(ACTIVITY_MAIN_OLD)
                }
                "质检管理" -> {
                    //Skipping.start(ACTIVITY_MAIN_OLD)
                }
                "采购管理" -> {
                    //Skipping.start(ACTIVITY_MAIN_OLD)
                }
            }
        }
    }

    /**
     * 权限检查
     */
    private fun authorityCheck() {

    }

}